package com.si.rapid.library.ApiFilestube;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

import org.apache.http.client.ClientProtocolException;


import org.apache.http.*;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;

import com.si.rapid.Globals;

public class Search {

	// Private Variables
	
	private String key="5eafe08c20933d0ddffd3d509fb766db";
	private HttpUriRequest Req=null;
	private HttpClient Interface=null;
	private HttpResponse Resp=null;
	private HttpResponse Resp_Consulta=null;
	private String consulta="";	
	private String XML="";
	private String Resp_Text=null;
	
	
	/**
	 * Ask API for a response about the query.
	 * @param phrase String of what are you searching.
	 * @param page Integer Maximum number of pages you want in the response.
	 * @throws URISyntaxException if the URI is not well generated.
	 */
	public void execute_search(String phrase,String related,String sort,int page,String extension){
		URI uri = null;
		URL url = null;
		
		
		try {
			uri = new URI("http", Globals.url,"/", "key="+key+"&phrase="+phrase.toString(),"");
		} catch (URISyntaxException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		try {
			url = uri.toURL();
		} catch (MalformedURLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		consulta=url.toString();
		Req=new HttpGet(consulta);
		Interface=new DefaultHttpClient();
		try {
			Resp=Interface.execute(Req);
		} catch (ClientProtocolException e1){
			e1.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
	
	/**
	 * Gets the Xml file in plane String (text).
	 * @return String of the XML.
	 */
	public String getXmlText(){
		
		ByteArrayOutputStream outstream = new ByteArrayOutputStream();
		byte [] responseBody ;
		String a;
		
		if(Resp!=null){
			
			try {
				Resp.getEntity().writeTo(outstream);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			responseBody = outstream.toByteArray();
			
			XML=a=new String(responseBody);
		}
		else {
			a=new String("Error");
			XML=null;
		}
		return a;
	}
	
	public String getUrlText(){
		
		ByteArrayOutputStream outstream = new ByteArrayOutputStream();
		byte [] responseBody ;
		String a;
		
		if(Resp_Consulta!=null){
			
			try {
				Resp_Consulta.getEntity().writeTo(outstream);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			responseBody = outstream.toByteArray();
			
			Resp_Text=a=new String(responseBody);
		}
		else {
			a=new String("Error");
			Resp_Text=null;
		}
		return a;
	}
	
	public void Consulta(String dir){
		URI uri = null;
		URL url = null;
		
		try {
			uri = new URI(dir);
		} catch (URISyntaxException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		
		try {
			url = uri.toURL();
		} catch (MalformedURLException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

		consulta=url.toString();
		Req=new HttpGet(consulta);
		Interface=new DefaultHttpClient();
		try {
			Resp_Consulta=Interface.execute(Req);
		} catch (ClientProtocolException e1){
			e1.printStackTrace();
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
	
}
